// src/store/authStore.js

import { defineStore } from 'pinia';

export const useAuthStore = defineStore('auth', {
    state: () => ({
        isAuthenticated: false,
        user: null,
    }),
    actions: {
        login(userData) {
            this.isAuthenticated = true;
            this.user = userData;
            localStorage.setItem('auth', JSON.stringify({ isAuthenticated: true, user: userData }));
        },
        logout() {
            this.isAuthenticated = false;
            this.user = null;
            localStorage.removeItem('auth');
        },
        initializeAuth() {
            const savedAuth = localStorage.getItem('auth');
            if (savedAuth) {
                const { isAuthenticated, user } = JSON.parse(savedAuth);
                this.isAuthenticated = isAuthenticated;
                this.user = user;
            }
        }
    }
});
